package net.sxnic.item.print.word;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import net.sxnic.comm.basecode.BaseCodeUtils;
import net.sxnic.comm.utils.CommUtils;
import net.sxnic.comm.utils.WordUtils;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itemgjhzunit.ItemGjhzunit;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemssmb.ItemSsmb;
import net.sxnic.taglib.BaseCodeUtilsCheckbox;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;

public class GjhzInfo {
	static {
		Document.setLicence("sj_laokai");
	}
	private static int SSMBROWINDEX = 12;
	private static int TABLEINDEXSSMB = 0;
	private static int SSMBROWMAX = 4;
	
	private static int GNROWINDEX = 9;
	private static int TABLEINDEXGN = 0;
	private static int GNROWMAX = 8;
	
	private static int GWROWINDEX = 17;
	private static int TABLEINDEXGW = 0;
	private static int GWROWMAX = 2;

	public static void createWord(String saveDocFile, String templateFile,
			Mdata mdata,ItemFund fund,ItemPrincipal principal,ItemClob clob, List<ItemGjhzunit> gwGjhzunitList, List<ItemGjhzunit> gnGjhzunitList,List<ItemPeople> gnList,List<ItemPeople> gwList,List<ItemSsmb> ssmbs, String basePath) {
		Document doc;
		try {
			File file = new File(saveDocFile);
			if (!file.getParentFile().exists()
					&& !file.getParentFile().mkdirs()) {
				return;
			}

			doc = new Document(templateFile);
			DocumentBuilder builder = new DocumentBuilder(doc);
			SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			BaseCodeUtilsCheckbox bcuc = new BaseCodeUtilsCheckbox();
			// 设置标签
			WordUtils.setBookmarks(doc, mdata);
			WordUtils.setBookmarks(doc, clob);
			WordUtils.setBookmarks(doc, fund);
			WordUtils.setBookmarks(doc, principal);
			
			//设置项目类别
			WordUtils.setBookmark(doc, "date", 
					CommUtils.FormatDateToString(new Date(), "yyyy年MM月dd日"));
			
			WordUtils.setBookmark(doc, "yn",
					BaseCodeUtils.getInfoName("001", mdata.getYn()));
			
			WordUtils.setBookmark(doc, "techFrom",
					BaseCodeUtils.getInfoName("016", mdata.getTechFrom()));
			
			WordUtils.setBookmark(doc, "techField",
					BaseCodeUtils.getInfoName("017", mdata.getTechField()));
			
			WordUtils.setBookmark(doc, "unitType",
					BaseCodeUtils.getInfoName("028", mdata.getUnitType()));
			
			WordUtils.setBookmark(doc, "govGjhz",
					BaseCodeUtils.getInfoName("001", mdata.getGovGjhz()));
			WordUtils.setBookmark(doc, "sex",
					BaseCodeUtils.getInfoName("002", principal.getSex()));
			WordUtils.setBookmark(doc, "unitProperty",
					BaseCodeUtils.getInfoName("005", mdata.getUnitProperty()));
			
			WordUtils.setBookmark(doc, "techTitle",
					BaseCodeUtils.getInfoName("014", principal.getTechTitle()));
			
			WordUtils.setBookmark(doc, "highestOffer",
					BaseCodeUtils.getInfoName("007", principal.getHighestOffer()));
			
			if(StringUtils.isNotBlank(mdata.getCheckContent())){
				WordUtils.setBookmark(doc, "checkContent",
						bcuc.splitInfoCode("021", mdata.getCheckContent()));
				WordUtils.setBookmark(doc, "total",
						String.valueOf(mdata.getCheckContent().split(",").length));
				
				}
			WordUtils.setBookmark(doc, "foreignCountry",
					BaseCodeUtils.getInfoName("004", principal.getForeignCountry()));
			if(StringUtils.isNotBlank(mdata.getZzdwCode())){
				WordUtils.setBookmark(doc,"zzdwCode",ItemConstants.ZZDW_MAP.get(ItemConstants.SYSTEM_CALALOG_ALIAS).get(mdata.getZzdwCode()));
				}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(0).getCoCountry())){
					WordUtils.setBookmark(doc, "gwCountry1",
							BaseCodeUtils.getInfoName("004",gwGjhzunitList.get(0).getCoCountry()));
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(0).getCoOrgname())){
					WordUtils.setBookmark(doc, "gwOrgname1",gwGjhzunitList.get(0).getCoOrgname());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(0).getCoLeader())){
					WordUtils.setBookmark(doc, "gwLeader1", gwGjhzunitList.get(0).getCoLeader());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(0).getCoPhone())){
					WordUtils.setBookmark(doc, "gwPhone1", gwGjhzunitList.get(0).getCoPhone());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(0).getCoFax())){
					WordUtils.setBookmark(doc, "gwFax1",  gwGjhzunitList.get(0).getCoFax());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(0).getCoEmail())){
					WordUtils.setBookmark(doc, "gwEmail1",  gwGjhzunitList.get(0).getCoEmail());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(1).getCoCountry())){
					WordUtils.setBookmark(doc, "gwCountry2",
							BaseCodeUtils.getInfoName("004", gwGjhzunitList.get(1).getCoCountry()));
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(1).getCoOrgname())){
					WordUtils.setBookmark(doc, "gwOrgname2", gwGjhzunitList.get(1).getCoOrgname());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(1).getCoLeader())){
					WordUtils.setBookmark(doc, "gwLeader2",  gwGjhzunitList.get(1).getCoLeader());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(1).getCoPhone())){
					WordUtils.setBookmark(doc, "gwPhone2", gwGjhzunitList.get(1).getCoPhone());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(1).getCoFax())){
					WordUtils.setBookmark(doc, "gwFax2", gwGjhzunitList.get(1).getCoFax());
			}
			if(StringUtils.isNotBlank(gwGjhzunitList.get(1).getCoEmail())){
					WordUtils.setBookmark(doc, "gwEmail2", gwGjhzunitList.get(1).getCoEmail());
					
				
			}
			
			if(StringUtils.isNotBlank(gnGjhzunitList.get(0).getCoOrgname())){
					WordUtils.setBookmark(doc, "gnOrgname1",gnGjhzunitList.get(0).getCoOrgname());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(0).getCoLeader())){
					WordUtils.setBookmark(doc, "gnLeader1", gnGjhzunitList.get(0).getCoLeader());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(0).getCoPhone())){
					WordUtils.setBookmark(doc, "gnPhone1", gnGjhzunitList.get(0).getCoPhone());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(0).getCoFax())){
					WordUtils.setBookmark(doc, "gnFax1",  gnGjhzunitList.get(0).getCoFax());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(0).getCoEmail())){
					WordUtils.setBookmark(doc, "gnEmail1",  gnGjhzunitList.get(0).getCoEmail());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(1).getCoOrgname())){
					WordUtils.setBookmark(doc, "gnOrgname2", gnGjhzunitList.get(1).getCoOrgname());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(1).getCoLeader())){
					WordUtils.setBookmark(doc, "gnLeader2",  gnGjhzunitList.get(1).getCoLeader());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(1).getCoPhone())){
					WordUtils.setBookmark(doc, "gnPhone2", gnGjhzunitList.get(1).getCoPhone());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(1).getCoFax())){
					WordUtils.setBookmark(doc, "gnFax2", gnGjhzunitList.get(1).getCoFax());
			}
			if(StringUtils.isNotBlank(gnGjhzunitList.get(1).getCoEmail())){
					WordUtils.setBookmark(doc, "gnEmail2", gnGjhzunitList.get(1).getCoEmail());
			}
			
			
			
			//项目分阶段实施目标

			if (ssmbs != null && ssmbs.size() > 0) {
				builder.moveToBookmark("ssmb_table");
				int tableIndex = TABLEINDEXSSMB;
				int j = SSMBROWINDEX;// 开始表格Row, 这里的index从0开始

				for (ItemSsmb ssmb : ssmbs) {
					// 实施时间区间
					builder.moveToCell(tableIndex, j, 0, 0);
					builder.write(ssmb.getStime()+"至"+ssmb.getEtime());
					// 实施内容
					builder.moveToCell(tableIndex, j, 1, 0);
					builder.write(ssmb.getContent());
					j++;
					if ((j - SSMBROWINDEX) >= SSMBROWMAX) {
						break;
					}
				}
			}
			//国内
			if (gnList != null && gnList.size() > 0 ) {
				builder.moveToBookmark("guonei_table");
				int tableIndex = TABLEINDEXGN;
				int j = GNROWINDEX;//开始表格Row,这里的index从0开始
			
				for (ItemPeople people : gnList) {
						   builder.moveToCell(tableIndex, j, 1, 0);  
						   builder.write(people.getChinName());
						   builder.moveToCell(tableIndex, j, 2, 0);
						   builder.write(BaseCodeUtils.getInfoName("002", people.getSex()));
						   builder.moveToCell(tableIndex, j, 3, 0);
						   builder.write(people.getBorn());
						   builder.moveToCell(tableIndex, j, 4, 0);
						   builder.write(BaseCodeUtils.getInfoName("006", people.getEducate()));
						   builder.moveToCell(tableIndex, j, 5, 0);
						   builder.write(BaseCodeUtils.getInfoName("024", people.getScienceTitle()));
						   builder.moveToCell(tableIndex, j, 6, 0);
						   builder.write(people.getSpecialty());
						   builder.moveToCell(tableIndex, j, 7, 0);
						   builder.write(people.getTask());
						   builder.moveToCell(tableIndex, j, 8, 0);
						   builder.write(people.getUnit());
						   builder.moveToCell(tableIndex, j, 9, 0);
						   builder.write(BaseCodeUtils.getInfoName("001", people.getPivotName()));
							j++;
							if ((j - GNROWINDEX) >= GNROWMAX) {
								break;
							}
						}
					}	
			//国外
				if (gwList != null && gwList.size() > 0 ) {
					builder.moveToBookmark("guowai_table");
					int tableIndex = TABLEINDEXGW;
					int j = GWROWINDEX;//开始表格Row,这里的index从0开始
				
					for (ItemPeople people : gwList) {
								builder.moveToCell(tableIndex, j, 1, 0);
								builder.write(people.getChinName());
								builder.moveToCell(tableIndex, j, 2, 0);
								builder.write(BaseCodeUtils.getInfoName("002", people.getSex()));
								builder.moveToCell(tableIndex, j, 3, 0);
								builder.write(people.getBorn());
								builder.moveToCell(tableIndex, j, 4, 0);
								builder.write(BaseCodeUtils.getInfoName("006", people.getEducate()));
								builder.moveToCell(tableIndex, j, 5, 0);
								builder.write(people.getSpecialty());
								builder.moveToCell(tableIndex, j, 6, 0);
								builder.write(BaseCodeUtils.getInfoName("024", people.getScienceTitle()));
								builder.moveToCell(tableIndex, j, 7, 0);
								builder.write(people.getTask());
								builder.moveToCell(tableIndex, j, 8, 0);
								builder.write(people.getUnit());
								builder.moveToCell(tableIndex, j, 9, 0);
								builder.write(BaseCodeUtils.getInfoName("001", people.getPivotName()));
								j++;
								if ((j - GWROWINDEX) >= GWROWMAX) {
									break;
								}
							}
						}

			doc.save(saveDocFile);
		} catch (Exception ex) {
			ex.printStackTrace();
			try {
				doc = new Document(templateFile);
				doc.save(saveDocFile);
			} catch (Exception e) {
				e.fillInStackTrace();
			}
		}
	}

}
